﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using MYA_WPF.Model;
using System.Diagnostics;


using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.TeamFoundation.MVVM;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using MYA_WPF.Utils;

namespace MYA_WPF.ViewModels
{
    public class EventWindowViewModel : ViewModel
    {
        public ObservableCollection<Type_Event> typeEvents { get; set; }
        public ObservableCollection<Personne> personnes { get; set; }
        public ObservableCollection<ContactDetail> contactDetailSelecteds { get; set; }
        public Type_Event vTypeEvent { get; set; }
        public Event vEvent { get; set; }
        public DateTime dateSelected { get; set; } 
        public BitmapImage vImageEvent { get; set; }
        public ObservableCollection<Contact> contacts { get; set; }
        private ManageYourAppointmentEntities dbcontext;
        private Personne personneSelected;
        

        public Personne PersonneSelected
        {
            get { return personneSelected; }
            set
            {
               
                if (personneSelected != value)
                {
                    personneSelected = value;
                    ContactDetail contactDetail = DataManager.getContactDetailByIdPersonne(personneSelected.id);
                    this.contactDetailSelecteds.Add(contactDetail);

                }
            }
        }
       


        public EventWindowViewModel()
        {
            dbcontext = new ManageYourAppointmentEntities();
            this.typeEvents = new ObservableCollection<Type_Event>(dbcontext.Type_Event.ToList<Type_Event>());// dbcontext.Type_Event.ToList<Type_Event>());
            this.personnes = new ObservableCollection<Personne>(dbcontext.Personnes.ToList<Personne>());
            this.contactDetailSelecteds = new ObservableCollection<ContactDetail>(); 
            vEvent = new Event();
            CreateSelectImageCommand();
            CreateEnregistrerCommand();
         
         

        }

        public ICommand EnregistrerCommand
        {
            get;
            internal set;
        }
        private void CreateEnregistrerCommand()
        {
            EnregistrerCommand = new RelayCommand(EnregistrerExecute, CanExecuteEnregistrerCommand);

        }

        private bool CanExecuteEnregistrerCommand(object parameter)
        {
            return !string.IsNullOrEmpty(vEvent.Nom_Event) && !string.IsNullOrEmpty(vEvent.Details) && vTypeEvent != null;
        }


        public void EnregistrerExecute(object parameter)
        {
          //vEvent.Contacts.Add()
             String pathimg = "";
            if (vImageEvent != null) {
           pathimg = vImageEvent.UriSource.AbsolutePath ;
            }
            //vEvent.Type_Event = vTypeEvent;
            DataManager.createEvent( vEvent,  contactDetailSelecteds.ToList<ContactDetail>(),  pathimg);
        }
        public ICommand SelectImageCommand
        {
            get;
            internal set;
        }
        private void CreateSelectImageCommand()
        {
            SelectImageCommand = new RelayCommand(SelectImageExecute);

        }
        public void SelectImageExecute(object parameter)
        {
            String selectedFile;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PNG Files|*.png";
           
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                selectedFile = openFileDialog.FileName.ToString(); 

                vImageEvent = new BitmapImage();
                try
                {
                    vImageEvent.BeginInit();
                    vImageEvent.CacheOption = BitmapCacheOption.OnLoad;
                    vImageEvent.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    vImageEvent.UriSource = new Uri(selectedFile, UriKind.Absolute);
                    vImageEvent.EndInit();
                    NotifyPropertyChanged("vImageEvent");
                }
                catch
                {

                }

            }

        }


    }
}
